package com.bf.xpdd.web.controller.setting;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.bf.xpdd.domain.ActivitySettingBean;
import com.bf.xpdd.domain.SiteSeller;
import com.bf.xpdd.service.ActivityService;
import com.bf.xpdd.service.ActivitySettingService;
import com.bf.xpdd.util.ActivityUtility;
import com.bf.xpdd.util.SettingUtility;

/**
 * Setting information
 * 
 * @author Fantastic
 * @version 1.0.0 2011-05-31
 * @since 1.0.0
 */
@Controller
@RequestMapping(value = "/setting")
public class SettingController
{
	/**
	 * 活动参数模板的服务
	 */
	@Autowired
	private ActivitySettingService settingService;

	/**
	 * 宝贝服务
	 */
	@Autowired
	private ActivityService activityService;

	/**
	 * default page
	 * 
	 * @return page
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String showDefault(HttpServletRequest request)
	{
		SiteSeller seller = ActivityUtility.getSellerFromSession(request.getSession());
		List<ActivitySettingBean> list = this.settingService.queryAllActSetting(seller.getNick());
		request.setAttribute("settingList", list);
		request.getSession().setAttribute("settingLength", list.size());
		//模板使用情况
		Map<Long, Boolean> map = new HashMap<Long, Boolean>();
		for (ActivitySettingBean bean : list)
		{
			map.put(bean.getId(), Boolean.FALSE);
		}
		List<Long> allIdList = this.activityService.queryAllModelIds(seller.getNick());
		for (Long id : allIdList)
		{
			map.put(id, Boolean.TRUE);
		}
		request.setAttribute("map", map);
		return "setting/set_list";
	}

	/**
	 * setting list
	 * 
	 * @return page
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/list")
	public String showSettingList(HttpServletRequest request)
	{
		SiteSeller seller = ActivityUtility.getSellerFromSession(request.getSession());
		List<ActivitySettingBean> list = this.settingService.queryAllActSetting(seller.getNick());
		request.setAttribute("settingList", list);
		request.getSession().setAttribute("settingLength", list.size());
		//模板使用情况
		Map<Long, Boolean> map = new HashMap<Long, Boolean>();
		for (ActivitySettingBean bean : list)
		{
			map.put(bean.getId(), Boolean.FALSE);
		}
		//查询当前用户使用的模板id
		List<Long> allIdList = this.activityService.queryAllModelIds(seller.getNick());
		for (Long id : allIdList)
		{
			map.put(id, Boolean.TRUE);
		}
		request.setAttribute("map", map);
		return "setting/set_list";
	}

	/**
	 * 察看已使用的模板
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/usedList")
	public String showUsedSettingList(HttpServletRequest request)
	{
		SiteSeller seller = ActivityUtility.getSellerFromSession(request.getSession());
		List<ActivitySettingBean> list = this.settingService.querySettingsByIsUsed(true, seller.getNick());
		request.setAttribute("settingList", list);
		//模板使用情况
		Map<Long, Boolean> map = new HashMap<Long, Boolean>();
		for (ActivitySettingBean bean : list)
		{
			map.put(bean.getId(), Boolean.TRUE);
		}
		request.setAttribute("map", map);
		return "setting/set_list";
	}

	/**
	 * 察看未使用的模板
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/unUsedList")
	public String showUnUsedSettingList(HttpServletRequest request)
	{
		SiteSeller seller = ActivityUtility.getSellerFromSession(request.getSession());
		List<ActivitySettingBean> list = this.settingService.querySettingsByIsUsed(false, seller.getNick());
		request.setAttribute("settingList", list);
		//模板使用情况
		Map<Long, Boolean> map = new HashMap<Long, Boolean>();
		for (ActivitySettingBean bean : list)
		{
			map.put(bean.getId(), Boolean.FALSE);
		}
		request.setAttribute("map", map);
		return "setting/set_list";
	}

	/**
	 * create activity
	 * 
	 * @return page
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/create")
	public String showCreateSetting(HttpServletRequest request)
	{
		Object tmpSize = request.getSession().getAttribute("settingLength");
		request.setAttribute("enable", true);
		if (tmpSize != null)
		{
			int size = ((Integer)tmpSize).intValue();
			if (size >= 8)
			{
				//达到个人最大模板数,不能创建新的模板
				request.setAttribute("enable", false);
			}
		}
		return "setting/set_create";
	}

	/**
	 * setting detail
	 * 
	 * @return page
	 */
	@RequestMapping(value = "/set_{id}", method = RequestMethod.GET)
	public String showSettingDetail(@PathVariable String id, HttpServletRequest request)
	{
		SiteSeller seller = ActivityUtility.getSellerFromSession(request.getSession());
		long setId = Long.parseLong(id);
		ActivitySettingBean setting = this.settingService.queryActSettingById(setId);
		if (!seller.getNick().equals(setting.getNick()))
		{
			request.setAttribute("error", "非法的请求");
			return "error";
		}
		request.setAttribute("setting", setting);
		//不可编辑
		request.setAttribute("editting", false);
		return "setting/set_detail";
	}

	/**
	 * 创建新的参数模板
	 */
	@RequestMapping(value = "addSetting", method = RequestMethod.POST)
	public String createSetting(@RequestParam Map<String, String> paramters,
			HttpServletRequest request)
	{
		//从参数中获取模板实例
		ActivitySettingBean settingBean = SettingUtility.getSettingFromParamters(paramters);
		if (settingBean == null)
		{
			return "error";
		}
		//转到所有模板察看页面
		SiteSeller seller = ActivityUtility.getSellerFromSession(request.getSession());
		//添加到数据库
		settingBean.setNick(seller.getNick());
		this.settingService.addActSetting(settingBean);

		List<ActivitySettingBean> list = this.settingService.queryAllActSetting(seller.getNick());
		request.setAttribute("settingList", list);
		request.getSession().setAttribute("settingLength", list.size());
		//模板使用情况
		Map<Long, Boolean> map = new HashMap<Long, Boolean>();
		for (ActivitySettingBean bean : list)
		{
			map.put(bean.getId(), Boolean.FALSE);
		}
		List<Long> allIdList = this.activityService.queryAllModelIds(seller.getNick());
		for (Long id : allIdList)
		{
			map.put(id, Boolean.TRUE);
		}
		request.setAttribute("map", map);
		return "setting/set_list";
	}
	
	/**
	 * 删除参数模板
	 */
	@RequestMapping(value = "/del_{id}", method = RequestMethod.GET)
	public String deleteSetting(@PathVariable String id, HttpServletRequest request)
	{
		SiteSeller seller = ActivityUtility.getSellerFromSession(request.getSession());
		long setId = Long.parseLong(id);
		this.settingService.delActSettingById(setId, seller.getNick());
		//删除后转到主页
		List<ActivitySettingBean> list = this.settingService.queryAllActSetting(seller.getNick());
		request.setAttribute("settingList", list);
		request.getSession().setAttribute("settingLength", list.size());
		//模板使用情况
		Map<Long, Boolean> map = new HashMap<Long, Boolean>();
		for (ActivitySettingBean bean : list)
		{
			map.put(bean.getId(), Boolean.FALSE);
		}
		List<Long> allIdList = this.activityService.queryAllModelIds(seller.getNick());
		for (Long tid : allIdList)
		{
			map.put(tid, Boolean.TRUE);
		}
		request.setAttribute("map", map);
		return "setting/set_list";
	}
	

	/**
	 * 编辑参数模板
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/edit_{id}", method = RequestMethod.GET)
	public String editSettingDetail(@PathVariable String id, HttpServletRequest request)
	{
		long setId = Long.parseLong(id);
		ActivitySettingBean setting = this.settingService.queryActSettingById(setId);
		request.setAttribute("setting", setting);
		//不可编辑
		request.setAttribute("editting", true);
		return "setting/set_detail";
	}

	/**
	 * 修改参数模板
	 */
	@RequestMapping(value = "modify", method = RequestMethod.POST)
	public String modifySetting(@RequestParam Map<String, String> paramters,
			HttpServletRequest request)
	{
		//从参数中获取模板实例
		ActivitySettingBean settingBean = SettingUtility.getSettingFromParamters(paramters);
		if (settingBean == null)
		{
			return "error";
		}
		settingBean.setId(Long.parseLong(paramters.get("setId")));
		//添加到数据库
		this.settingService.modifyActSetting(settingBean);

		//转到所有模板察看页面
		SiteSeller seller = ActivityUtility.getSellerFromSession(request.getSession());
		List<ActivitySettingBean> list = this.settingService.queryAllActSetting(seller.getNick());
		request.setAttribute("settingList", list);
		request.getSession().setAttribute("settingLength", list.size());
		//模板使用情况
		Map<Long, Boolean> map = new HashMap<Long, Boolean>();
		for (ActivitySettingBean bean : list)
		{
			map.put(bean.getId(), Boolean.FALSE);
		}
		List<Long> allIdList = this.activityService.queryAllModelIds(seller.getNick());
		for (Long id : allIdList)
		{
			map.put(id, Boolean.TRUE);
		}
		request.setAttribute("map", map);
		return "setting/set_list";
	}
}
